package com.cs.user.application.controller;

import com.cs.user.application.dtos.UserDTO;

import com.cs.user.common.bos.AuthUserBO;
import com.cs.user.common.entity.BaseResponse;
import com.cs.user.common.entity.PageResult;
import com.cs.user.common.utlis.ResultUtils;
import com.cs.user.domain.bos.UserBO;
import com.cs.user.domain.service.UserDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.stream.Collectors;

/**
 * 用户模块
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserDomainService userDomainService;

    /**
     * 用户注册
     */
    @PostMapping("/register")
    public BaseResponse<Boolean> register(@RequestBody AuthUserBO authUserBO) {
        // 输出日志
        log.info("feign-register传来的参数:{}", authUserBO);
        // 调用domain层方法
        return ResultUtils.success(userDomainService.register(authUserBO));
    }

    /**
     * 用户登录
     */
    @PostMapping("/login")
    public BaseResponse<AuthUserBO> login(@RequestBody AuthUserBO authUserBO) {
        // 输出日志
        log.info("feign-login传来的参数:{}", authUserBO);
        // 调用domain层方法
        AuthUserBO loginUser = userDomainService.login(authUserBO);
        return ResultUtils.success(loginUser);
    }

    /**
     * 更新用户信息
     */
    @PostMapping("/update")
    public BaseResponse<Boolean> updateUser(@RequestBody UserDTO userDTO) {
        // 输出日志
        log.info("更新用户参数:{}", userDTO);
        // 类型转换
        UserBO userBO = new UserBO();
        BeanUtils.copyProperties(userDTO, userBO);
        return ResultUtils.success(userDomainService.updateUser(userBO));
    }

    /**
     * 删除用户
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteUser(@RequestBody UserDTO userDTO) {
        // 输出日志
        log.info("删除用户参数:{}", userDTO);
        // 类型转换
        UserBO userBO = new UserBO();
        BeanUtils.copyProperties(userDTO, userBO);
        return ResultUtils.success(userDomainService.deleteUser(userBO));
    }

    /**
     * 分页查询用户
     */
    @PostMapping("/page")
    public BaseResponse<PageResult<UserDTO>> pageUser(@RequestBody UserDTO userDTO) {
        // 输出日志
        log.info("分页查询用户参数:{}", userDTO);
        // 类型转换
        UserBO userBO = new UserBO();
        BeanUtils.copyProperties(userDTO, userBO);
        // 调用domain层方法
        PageResult<UserBO> authUserBOList = userDomainService.pageUser(userBO);
        // 类型转换
        PageResult<UserDTO> pageResult = new PageResult<>();
        pageResult.setTotal(authUserBOList.getTotal());
        pageResult.setRecords(authUserBOList.getResult().stream().map(item -> {
            UserDTO userDto = new UserDTO();
            BeanUtils.copyProperties(item, userDto);
            return userDto;
        }).collect(Collectors.toList()));
        return ResultUtils.success(pageResult);
    }

    /**
     * 条件查询
     */
    @PostMapping("/queryUserByAccount")
    public BaseResponse<UserDTO> queryUserByAccount(@RequestBody UserDTO userDTO) {
        // 输出日志
        log.info("条件查询用户参数:{}", userDTO);
        // 类型转换
        UserBO userBO = new UserBO();
        BeanUtils.copyProperties(userDTO, userBO);
        // 调用domain层方法
        UserBO userBo = userDomainService.queryUserByAccount(userBO);
        // 类型转换
        UserDTO userDto = new UserDTO();
        BeanUtils.copyProperties(userBo, userDto);
        return ResultUtils.success(userDto);
    }
}
